///*const int numberOfMonth = 12;
	//double sum = 0.0;
	//for (int i = 0; i < numberOfMonth; i++) {
	//	double clsBln;
	//	cin>>clsBln;
	//	sum += clsBln;
	//}

	//double ave = sum / 12;
	//cout<<"$";
	//cout<<setiosflags(ios::fixed);
	//cout<<setprecision(2);
	//cout<<ave<<endl;
	//return 0;*/
	//const double PI = 3.14159265359;
	//int N;
	//cin>>N;
	//int v = 1; 
	//while (v <= N)
	//{
	//	double year = 1.0;
	//	double x, y;
	//	cin>>x>>y;
	//	year = PI * (x * x + y * y) / 100;
	//	int nYear = (int)year + 1;
	//	
	//	cout<<"Property "<<v<<": This property will begin eroding in year "<<nYear<<"."<<endl;

	//	++v;
	//}
	//cout<<"END OF OUTPUT."<<endl;
	//return 0;
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char* argv[])
{
	const int pp = 23, ep = 28, ip = 33;
	int caseN = 1;
	int p, e, i, d;
	cin>>p>>e>>i>>d;
	while (p != -1 && e != -1 && i != -1 && d != -1)
	{
		p %= pp;
		e %= ep;
		i %= ip;
		int k = 0, n = 0, l = 0;
		while (true)
		{
			int res = (e - p) + ep * k;
			int res1 = (e - i) + ep * k;
			if (res % pp == 0 && res1 % ip == 0) {
				n = res / pp;
				l = res1 / ip;
				break;
			}
			++k;
		}
		int result = p + n * pp;
		if (result <= d) {
			result = pp * ep * ip - d + result;
		} else {
			result -= d;
		}
		cout<<"Case "<<caseN<<": the next triple peak occurs in "<<result<<" days."<<endl;
		++caseN;
		cin>>p>>e>>i>>d;
	}
}
